package com.tuojian.nanjing.healthysystem.controller;

import com.baomidou.mybatisplus.extension.service.IService;
import com.tuojian.nanjing.healthysystem.common.Result;
import com.tuojian.nanjing.healthysystem.entity.CheckIn;
import com.tuojian.nanjing.healthysystem.entity.ExerciseRecord;
import com.tuojian.nanjing.healthysystem.entity.WeightRecord;
import com.tuojian.nanjing.healthysystem.service.CheckInService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/api/health")
@Api(tags = "健康管理接口")
public class HealthController {

    @Autowired
    private CheckInService checkInService;

//    @Autowired
//    private IService<WeightRecord> weightRecordService; // 通用 IService
//
//    @Autowired
//    private IService<ExerciseRecord> exerciseRecordService;

    @PostMapping("/checkin")
    @ApiOperation("用户每日打卡")
    public Result<?> checkIn(@RequestParam Long userId) {
        try {
            CheckIn record = checkInService.doCheckIn(userId);
            return Result.success(record);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

//    @PostMapping("/weight")
//    @ApiOperation("记录体重")
//    public Result<?> addWeight(@RequestBody WeightRecord record) {
//        weightRecordService.save(record);
//        return Result.success(record);
//    }

    @GetMapping("/checkin/history")
    @ApiOperation("获取打卡历史")
    public Result<?> checkInHistory(
            @RequestParam Long userId,
            @RequestParam(defaultValue = "7") int days) {
        LocalDate end = LocalDate.now();
        LocalDate start = end.minusDays(days - 1);
        List<CheckIn> list = checkInService.getCheckInHistory(userId, start, end);
        return Result.success(list);
    }
}